package com.student.recruitment.app.controller;

import java.io.IOException;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.student.recruitment.app.entity.EmployeeEntity;
import com.student.recruitment.app.service.EmployeeManager;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class EditStudentController
{	
    @Autowired
    private EmployeeManager employeeManager;
     
    @RequestMapping(value = {"/criteria" }, method = RequestMethod.POST)
	public String criteria(
			@RequestParam(value = "kryteria", required = false) String kryteria,
			@RequestParam(value = "nazwa", required = false) String nazwa,
			ModelMap map) {
 
    	map.addAttribute("employee", new EmployeeEntity());
    	map.addAttribute("employeeList", employeeManager.getMyCriteriaRecords(kryteria, nazwa));
    	
		return "editEmployeeList";
 
	}
    @RequestMapping(value = {"/sort" }, method = RequestMethod.POST)
	public String sort(
			@RequestParam(value = "direction", required = false) String direction,
			@RequestParam(value = "points", required = false) int points,
			ModelMap map) {
 
    	map.addAttribute("employee", new EmployeeEntity());
    	map.addAttribute("employeeList", employeeManager.SortResultsDirectionOfPoints(direction, points));
    	
		return "editEmployeeList";
 
	}
    @RequestMapping(value = {"/wyniki" }, method = RequestMethod.POST)
	public String wyniki(
			@RequestParam(value = "direction", required = false) String direction,
			@RequestParam(value = "ilosc", required = false) int ilosc,
			ModelMap map) {
 
    	map.addAttribute("employee", new EmployeeEntity());
    	map.addAttribute("employeeList", employeeManager.SortResultsDirectionOfAmount(direction, ilosc));
    	
		return "editEmployeeList";
 
	}
    
    @RequestMapping(value = "/baza", method = RequestMethod.GET)
    public String listEmployees(ModelMap map)
    {
        map.addAttribute("employee", new EmployeeEntity());
        map.addAttribute("employeeList", employeeManager.getAllEmployees());
        return "editEmployeeList";
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addEmployee(@Valid @ModelAttribute(value="employee") EmployeeEntity employee, BindingResult result) throws IOException
    {
    	if (result.hasErrors()) {
            //System.out.println("dsd");
            return "register";
        }
    	
        employeeManager.addEmployee(employee);
        return "redirect:/";
    }
    
    
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String users(ModelMap map)
    {
    	map.addAttribute("employee", new EmployeeEntity());
        map.addAttribute("employeeList", employeeManager.getAllEmployees());
        return "users";
    }
    
    @RequestMapping(value = "/edit/{employeeId}" , method = RequestMethod.GET)
    public String getEmployee(@ModelAttribute EmployeeEntity employee, BindingResult result, @PathVariable("employeeId") Integer employeeId, ModelMap map)
    {
    	map.addAttribute("employee", employeeManager.getEmployee(employeeId));
    	map.addAttribute("employeeList", employeeManager.getAllEmployees());
        
        return "editEmployeeList";
    }
    @RequestMapping("/accept/{employeeId}")
    public String acceptEmployee(@PathVariable("employeeId") Integer employeeId)
    {
        employeeManager.acceptEmployee(employeeId);
        
        return "redirect:/baza";
    }
    
    @RequestMapping("/delete/{employeeId}")
    public String deleteEmployee(@PathVariable("employeeId") Integer employeeId)
    {
        employeeManager.deleteEmployee(employeeId);
        return "redirect:/baza";
    }
    }